<script setup>
import { onBeforeRouteUpdate } from 'vue-router'
import {
  Bell,
  CaretBottom, ChatDotSquare,
  Crop,
  EditPen, Goods, HomeFilled, House,
  Management, Menu, Message, OfficeBuilding, Operation,
  Promotion, Star,
  SwitchButton,
  User,
  UserFilled
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import {userInfoService} from "@/api/user";
import {useUserInfoStore} from "@/stores/userInfo";
import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import {ref} from "vue";
import {useMerchantInfoStore} from "@/stores/merchantInfo";
import {getCurMerchant} from "@/api/merchant";

// 获取当前登录的商家的信息
const merchantInfoStore = useMerchantInfoStore();
const getMerchantInfo = () => {
  getCurMerchant().then(res => {
    if (!res.data) {
      ElMessage({
        message: '暂未登录，请先登录',
        type: 'warning',
      })
      router.push({path: '/merchantLoginPage'})
    } else {
      merchantInfoStore.setMerchantInfo(res.data)
    }
  })
}
getMerchantInfo()

const router = useRouter();
const logoutMerchant = () => {
  ElMessageBox.confirm(
      '你确认要退出吗？',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(
      async () => {
        merchantInfoStore.removeMerchantInfo()
        ElMessage.success("退出成功")
        await router.push('/merchantLoginPage')
      }
  )
}

const activeIndex = ref('1')
const handleSelect = (key, keyPath) => {
  console.log(key, keyPath)
}
</script>

<template>
  <el-container class="layout-container">

    <el-header>
      <!-- 放置统一的logo -->
      <div class="logo-container">
        <!-- 与首页一致的对称式logo：对话气泡+单星星 -->
        <svg
            class="logo-icon"
            viewBox="0 0 48 48"
            fill="none"
        >
          <!-- 外层对称气泡形状 -->
          <path d="M12 24C12 15.1634 19.1634 8 28 8C36.8366 8 44 15.1634 44 24C44 32.8366 36.8366 40 28 40C23.5817 40 19.5291 38.2426 16.5858 35.2993L12 40V24Z"
                stroke="#1E40AF" stroke-width="2.5" fill="#EFF6FF"/>

          <!-- 中央评分星星 -->
          <path d="M24 14L26.5 22L34 22L28 26.5L30 34L24 30L18 34L20 26.5L14 22L21.5 22L24 14Z"
                fill="#1E40AF"/>
        </svg>
        <span class="logo-text">点评通</span>
      </div>
    </el-header>
    <!--  顶部导航  -->
    <el-menu
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        router
    >
      <div class="welcome">
        欢迎商家：<strong>{{ merchantInfoStore.merchantInfo.username }}</strong>
      </div>
      <el-menu-item
          index="/merchant/info"
          class="nav-item"
      >
        <el-icon>
          <Promotion/>
        </el-icon>
        <span>基本信息</span>
      </el-menu-item>
      <el-menu-item
          index="/merchant/storeManageList"
          class="nav-item"
      >
        <el-icon>
          <OfficeBuilding/>
        </el-icon>
        <span>我的店铺</span>
      </el-menu-item>
      <el-sub-menu index="2" class="nav-item submenu-item">
        <template #title>
          <el-icon>
            <Message/>
          </el-icon>
          <span>消息</span>
        </template>
        <el-menu-item
            index="/merchant/fansInteraction"
            class="submenu-child"
        >
          <el-icon>
            <Star/>
          </el-icon>
          <span>贴文动态</span>
        </el-menu-item>
        <el-menu-item
            index="/merchant/systemMessage"
            class="submenu-child"
        >
          <el-icon>
            <Bell/>
          </el-icon>
          <span>系统消息</span>
        </el-menu-item>
      </el-sub-menu>
      <el-menu-item
          @click="logoutMerchant"
          class="nav-item logout"
      >
        <el-icon><SwitchButton /></el-icon>
        <span>退出登录</span>
      </el-menu-item>
    </el-menu>

    <!-- 下方主区域 -->
    <el-container>
      <!-- 中间区域 -->
      <el-main>
        <router-view/>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>商家管理系统 ©2025</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.welcome {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: #ffffff;
  background-color: #419fff;
}

// Logo样式
.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 100%;

  .logo-icon {
    width: 44px;
    height: 44px;
    transition: transform 0.3s ease, filter 0.3s ease;

    &:hover {
      transform: rotate(5deg) scale(1.05);
      filter: drop-shadow(0 0 8px rgba(30, 64, 175, 0.3));
    }
  }

  .logo-text {
    font-size: 22px;
    font-weight: 600;
    color: #1E40AF;
  }
}

.layout-container {
  height: 100vh;

  .el-header {
    background-color: #ecf4ff;
    display: flex;
    align-items: center;
    justify-content: flex-start; /* 调整为左对齐 */
    padding: 0 24px; /* 添加内边距 */
    //********************justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}

// 导航按钮样式优化
.el-menu-demo {
  .nav-item {
    // 统一按钮高度和内边距
    height: 60px !important;
    line-height: 60px !important;
    padding: 0 18px !important;
    position: relative;
    transition: all 0.3s ease;

    // 图标样式优化
    .el-icon {
      margin-right: 8px;
      font-size: 16px;
      transition: transform 0.3s ease;
    }

    // 悬停效果
    &:hover {
      background-color: rgba(255, 255, 255, 0.1) !important;

      .el-icon {
        transform: scale(1.1);
      }
    }

    // 选中状态
    &.is-active {
      background-color: rgba(255, 255, 255, 0.15) !important;

      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 3px;
        background-color: #ffffff;
      }
    }
  }

  // 子菜单样式
  .submenu-item {
    .el-sub-menu__title {
      height: 60px !important;
      line-height: 60px !important;
    }
  }

  // 子菜单选项
  .submenu-child {
    height: 45px !important;
    line-height: 45px !important;

    &:hover {
      background-color: #ecf4ff !important;
      color: #419fff !important;
    }
  }

  // 退出登录按钮
  .logout {
    margin-left: 15px;
    border-left: 1px solid rgba(255, 255, 255, 0.1);

    &:hover {
      background-color: rgba(239, 68, 68, 0.1) !important;
    }
  }
}
</style>